package com.chart.chart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.ChartTheme;
import com.chart.chart.mapper.ChartConfigsMapper;
import com.chart.chart.mapper.ChartThemeMapper;
import com.chart.chart.service.IChartConfigsService;
import com.chart.chart.service.IChartThemeService;
import com.chart.common.utils.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class ChartThemeServiceImpl extends ServiceImpl<ChartThemeMapper, ChartTheme> implements IChartThemeService {
    @Autowired
    private ChartThemeMapper chartThemeMapper;

    @Override
    public void colorSave(String themeName) {
        if(StringUtils.isNotEmpty(themeName)){
            LambdaQueryWrapper<ChartTheme> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ChartTheme::getThemeName, themeName);
            List<ChartTheme> chartThemes = chartThemeMapper.selectList(lqw);
            ChartTheme chartTheme = chartThemes.get(0);
            if(chartTheme!=null){
                chartTheme.setSave(chartTheme.getSave()+1);
                chartThemeMapper.updateById(chartTheme);
            }
        }



//        if(StringUtils.isNotEmpty(themeName)){
//        LambdaQueryWrapper<ChartTheme> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(ChartTheme::getThemeName, themeName);
//        List<ChartTheme> chartThemes = chartThemeMapper.selectList(lqw);
//        ChartTheme chartTheme = chartThemes.get(0);
//        if(chartTheme!=null){
//            chartTheme.setSave(chartTheme.getSave()+1);
//            chartThemeMapper.updateById(chartTheme);
//        }
//        }
    }

    @Override
    public void colorExport(String themeName) {
        if(StringUtils.isNotEmpty(themeName)){
        LambdaQueryWrapper<ChartTheme> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ChartTheme::getThemeName, themeName);
        List<ChartTheme> chartThemes = chartThemeMapper.selectList(lqw);
        ChartTheme chartTheme = chartThemes.get(0);

        if(chartTheme!=null){
            chartTheme.setExport(chartTheme.getExport()+1);
            chartThemeMapper.updateById(chartTheme);
        }

        }
    }

//    @Override
//    public List<ChartTheme> querySaveExportPv() {
//        List<ChartTheme> chartThemeList = chartThemeMapper.querySaveExportPv();
//        for (ChartTheme chartTheme : chartThemeList) {
//            String themeValue = chartTheme.getThemeValue();
//            try {
//                ObjectMapper objectMapper = new ObjectMapper();
//                JsonNode jsonNode = objectMapper.readTree(themeValue);
//                JsonNode color = jsonNode.path("color");
//                // 将 color 节点转换为 String
//                String colors = color.asText(); // 转换为 String
//
//
//
//            } catch (JsonProcessingException e) {
//                throw new RuntimeException(e);
//            }
//
//        }
//
//        return chartThemeList;
//    }

    @Override
    public List<ChartTheme> querySaveExportPv() {
        List<ChartTheme> chartThemeList = chartThemeMapper.querySaveExportPv();

        for (ChartTheme chartTheme : chartThemeList) {
            String themeValue = chartTheme.getThemeValue();
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode jsonNode = objectMapper.readTree(themeValue);
                JsonNode colorNode = jsonNode.path("color");
                List<String> colorsList = new ArrayList<>();
                if (colorNode.isArray()) {
                    for (JsonNode color : colorNode) {
                        colorsList.add(color.asText());
                    }
                }
                chartTheme.setColors(colorsList);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
        return chartThemeList;
    }

    @Override
    public List<ChartTheme> getChartThemes() {
        return this.list(); // MyBatis-Plus 提供的查询所有方法
    }


}
